package com.lin.server.impl;

import com.lin.dataobject.SellerInfo;
import com.lin.repository.SellerInfoRepository;
import com.lin.server.SellerService;
import com.lin.utils.DateUtils;
import com.lin.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 2017-07-29 23:15
 */
@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private SellerInfoRepository repository;


    @Override
    public SellerInfo findByUserName(String userName) {
        return repository.findByUsername(userName);
    }

    @Override
    public SellerInfo findByUsernameAndPassword(String userName, String password) {
        return repository.findByUsernameAndPassword(userName,password);
    }

    @Override
    public List<SellerInfo> findAll(String username, String startTime, String endTime) {
        return repository.findAll(getSpecification(username,startTime,endTime));
    }

    @Override
    public Page<SellerInfo> findAll(String username, String startTime, String endTime, Pageable pageable) {
        return repository.findAll(getSpecification(username,startTime,endTime),pageable);
    }

    @Override
    public SellerInfo findUserById(String userId) {
        return repository.findBySellerId(userId);
    }

    @Override
    public SellerInfo save(SellerInfo sellerInfo) {
        return repository.save(sellerInfo);
    }

    @Override
    public void del(SellerInfo sellerInfo) {
        repository.delete(sellerInfo);
    }

    @Override
    public Long getSize(String username, String startTime, String endTime) {
        return repository.count(getSpecification(username,startTime,endTime));
    }

    //组合条件查询
    private Specification<SellerInfo> getSpecification(String username, String startTime, String endTime){
        return new Specification<SellerInfo>() {
            @Override
            public Predicate toPredicate(Root<SellerInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if (StringUtils.isNotEmpty(username)){
                    predicates.add(criteriaBuilder.like(root.get("username"),"%"+username+"%"));
                }

                if (StringUtils.isNotEmpty(startTime)){
                    Date start= DateUtils.stringToDate(startTime);
                    predicates.add(criteriaBuilder.greaterThan(root.get("createTime"),start));
                }
                if (StringUtils.isNotEmpty(endTime)){
                    Date end=DateUtils.addDay(endTime,1);
                    predicates.add(criteriaBuilder.lessThan(root.get("createTime"),end));
                }
                return criteriaQuery.where(predicates.toArray
                        (new Predicate[predicates.size()])).getRestriction();
            }
        };
    }
}
